package fr.umlv.escapeir.painters;

import java.awt.Graphics2D;
import java.awt.Image;
import java.io.IOException;

import fr.umlv.escapeir.game.Behavior;

public class ScrollingImageLayer extends AnimatedLayer {
	private final Image img;
	private final int layerWidth;
	private final int layerHeight;
	private float currentY;
	private Behavior stepper;
	
	public ScrollingImageLayer (String filename, int width, int height, final float scrollSpeed) throws IOException {
		this.img = ImageLoader.load(filename);
		this.layerWidth = width;
		this.layerHeight = height;
		this.currentY = this.img.getHeight(null)-layerHeight;
		this.stepper = new Behavior () {
			@Override
			public void apply() {
				currentY = (currentY >= 0 ? currentY-scrollSpeed : img.getHeight(null)-layerHeight);
			}
		};
	}

	@Override
	public void render(Graphics2D painter) {
		painter.drawImage(img, 0, 0, 
				layerWidth, layerHeight,
				0, (int)currentY,
				layerWidth, (int)currentY+layerHeight, null);
	}

	@Override
	public void apply() {
		stepper.apply();
	}
	
}
